export const chartTitle = (text) => {
  if (!text) return undefined
  return {
    text: `{placeholder| }{title|${text}}`,
    left: 'left',
    top: '12px',
    textStyle: {
      rich: {
        title: {
          color: '#181818',
          fontSize: 14,
          fontWeight: 400,
          padding: [0, 0, 0, 8],
        },
        placeholder: {
          width: 4,
          height: 12,
          backgroundColor: '#939393',
          left: '-4',
        },
      },
    },
  }
}
export const getCssVariableValue = (variableName) => {
  return getComputedStyle(document.documentElement).getPropertyValue(variableName).trim()
}
// 十六进制转rgb
export const hexToRgb = (hex) => {
  // 去掉开头的 # 号
  hex = hex.replace(/^#/, '')

  // 处理三位数的情况 (#FFF)
  if (hex.length === 3) {
    hex = hex
      .split('')
      .map(function (hex) {
        return hex + hex
      })
      .join('')
  }

  // 提取红色、绿色和蓝色部分
  const bigint = parseInt(hex, 16)
  const r = (bigint >> 16) & 255
  const g = (bigint >> 8) & 255
  const b = bigint & 255

  return 'rgb(' + r + ',' + g + ',' + b + ')'
}
// 得到一个随机色
export const getRandomColor = () => {
  const randomChannel = () => Math.floor(Math.random() * 256)
  return `rgb(${randomChannel()}, ${randomChannel()}, ${randomChannel()})`
}

// 得到传来参数颜色的相近色
export const lightenColor = (rgb, factor = 0.2) => {
  const colorValues = rgb.match(/\d+/g).map(Number)
  const lightenValue = (value) => Math.min(255, Math.floor(value * (1 + factor)))
  return `rgb(${lightenValue(colorValues[0])}, ${lightenValue(colorValues[1])}, ${lightenValue(colorValues[2])})`
}
